//
//  GYDKeyboardPackage.swift
//  Weibo
//
//  Created by 贵永冬 on 16/8/2.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit
import YYModel

class GYDKeyboardPackage: NSObject,YYModel {
    var id : String?//文件夹
    var group_name_cn : String? //当前组的名称
    var emoticons : [GYDKeyboardEmoticon]?//组的所有表情
    
    class  func loadEmoticonPackages() -> [GYDKeyboardPackage] {
        var models = [GYDKeyboardPackage]()
        let package = GYDKeyboardPackage()
        package.appendEmptyEmoticons()
        models.append(package)
        let path  = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")
        let dic = NSDictionary(contentsOfFile: path!)
        let array = dic!["packages"] as! [[String : AnyObject]]
        
        for packageDic in array {
            let id = packageDic["id"] as! String
            let model = self.loadEmoticons(id)
            model.id = id
            model.appendEmptyEmoticons()
            models.append(model)
        }
        return models
    }
    
    private func appendEmptyEmoticons() {
        //判断是否是最近的
        if  emoticons == nil {
            emoticons = [GYDKeyboardEmoticon]()
            
        }
        
        let number = (emoticons?.count)! % 21
        //补全
        for _ in number ..< 20 {
            let emotiocon = GYDKeyboardEmoticon()
            emotiocon.isRemoveButton = false
            emoticons?.append(emotiocon)
        }
        let emotiocon = GYDKeyboardEmoticon()
        emotiocon.isRemoveButton = true
        emoticons?.append(emotiocon)
    }
    class func loadEmoticons(id: String) -> GYDKeyboardPackage {
        let path = NSBundle.mainBundle().pathForResource(id, ofType: nil, inDirectory: "Emoticons.bundle")!
        let filePath = (path as NSString).stringByAppendingPathComponent("info.plist")
        let dic = NSDictionary(contentsOfFile: filePath)
        let model = GYDKeyboardPackage.yy_modelWithJSON(dic!)
        var index = 0
        var emoticons = [GYDKeyboardEmoticon]()
        for emoticon in (model?.emoticons)! {
            if index == 20 {
                let emoticonD = GYDKeyboardEmoticon()
                emoticonD.isRemoveButton = true
                index = 0
                emoticons.append(emoticonD)
            }else {
                emoticon.id = model?.id
                emoticons.append(emoticon)
                index += 1
            }
        }
        model?.emoticons = emoticons
        return model!
    }
    
    func addNewEmoticon(emoticon:GYDKeyboardEmoticon) {
        if emoticons!.contains(emoticon) {
            return
        }
        //添加
        emoticons?.insert(emoticon, atIndex: 0)
        if emoticons?.count > 21 {
            emoticons?.removeAtIndex(emoticons!.count - 2)
        }
    }
    
    static func modelContainerPropertyGenericClass() -> [String : AnyObject]? {
        return ["emoticons":GYDKeyboardEmoticon.classForCoder()]
    }
    
}
class GYDKeyboardEmoticon: NSObject {
    var id : String? {
        didSet {
            let path = NSBundle.mainBundle().pathForResource(id, ofType: nil, inDirectory: "Emoticons.bundle")!
            pngPath = (path as NSString).stringByAppendingPathComponent(png ?? "")
        }
    }
    var chs : String?//对应得字符串
    var png : String? 
    //Emopticon 对应的字符串
    var code : String? {
        didSet {
            let scanner = NSScanner(string: code ?? "")
            var result : UInt32 = 0
            scanner.scanHexInt(&result)
            emoticonStr = "\(Character(UnicodeScalar(result)))"
        }
    }
    var emoticonStr : String?//转换之后的emoji表情
    var pngPath : String? // 图片的绝对路径
    var isRemoveButton : Bool = false
    
}





